package com.kangfawei.io;

import java.io.*;
import java.net.URL;

/**
 * @author 康发伟
 * 爬取网页的源代码并保存到文件中
 */
public class   ConvertTest2 {
    public static void main(String[] args) {
        String url = "https://www.zhihu.com";
        String target = "E:/IO_target/zhihu.html";

        getFromUtl(url,target);
    }

    public static void getFromUtl(String url,String target){
        try(BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(url).openStream()));
            BufferedWriter writer = new BufferedWriter(new FileWriter(target))) {

            String msg = "";
            while ((msg = reader.readLine())!=null){
                writer.write(msg);
                writer.newLine();
            }
            writer.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
